package com.cpi.cornpp.purchaseorder.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.futuresversions.exception.CornFuturesVersionsException;
import com.cpi.cornpp.futuresversions.services.CornFuturesVersionsService;
import com.cpi.cornpp.futuresversions.view.CornFuturesVersionsView;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.form.PurchaseOrderVersionSortForm;
import com.cpi.cornpp.purchaseorder.service.PurchaseOrderVersionService;

public class SortPurchaseOrderAction extends MappingDispatchAction {
	private static final String FORWARD_DEFAULT = "default";//$NON-NLS-1$
	private static final String ATTRIBUTE_VERSION_ID = "versionId";//$NON-NLS-1$
	private static final String ATTRIBUTE_PERIODS_VERSION = "futurePeriodsVersion";//$NON-NLS-1$
	private static final String ATTRIBUTE_PURCHASES_ORDERS_VERSION = "purchasesOrdersVersion";//$NON-NLS-1$
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ATTRIBUTE_PURCHASES_ORDERS_VERSION_SIZE = "purchasesOrdersVersionSize";//$NON-NLS-1$
	private static final String ATTRIBUTE_FUTURE_PERIOD_VERSION_ID = "futurePeriodVersionId";//$NON-NLS-1$
	private static final String PARAMETER_SAVE_FUTURE_PERIOD_VERSION_ID = "saveFuturePeriodVersionId";//$NON-NLS-1$
	private static final String PARAMETER_PURCHASE_ORDER_VERSION_ID = "purchaseOrderVersionId";//$NON-NLS-1$
	private static final String PARAMETER_DIRECTION = "direction";//$NON-NLS-1$
	public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws PurchaseProgramException, CornFuturesVersionsException {
		CornFuturesVersionsService cornFuturesVersionsService = null;
		PurchaseOrderVersionService purchaseOrderVersionService = null;
		HttpSession httpSession = null;
		String strVersionId;
		int versionId;
		int futurePeriodVersionId;
		String strFuturePeriodVersionId = null;
		List futurePeriodsVersion = null;
		List purchaseOrderVersions = null;
		PurchaseOrderVersionSortForm purchaseOrderVersionSortForm = null;
		CornFuturesVersionsView cornFuturesVersionsView = null;
		try{
			purchaseOrderVersionSortForm = (PurchaseOrderVersionSortForm)form;
			cornFuturesVersionsService = new CornFuturesVersionsService();
			purchaseOrderVersionService = new PurchaseOrderVersionService();
			httpSession = request.getSession();
			strVersionId = (String)httpSession.getAttribute(ATTRIBUTE_VERSION_ID);
			versionId = Integer.parseInt(strVersionId);
			futurePeriodsVersion = cornFuturesVersionsService.cornFuturesPeriodsVersion(versionId);
			if( StringUtils.isNotEmpty(purchaseOrderVersionSortForm.getFuturePeriodVersionId()) ){
				futurePeriodVersionId = Integer.parseInt(purchaseOrderVersionSortForm.getFuturePeriodVersionId());
			}else{
				strFuturePeriodVersionId = (String)request.getAttribute(ATTRIBUTE_FUTURE_PERIOD_VERSION_ID);
				if( StringUtils.isNotEmpty(strFuturePeriodVersionId) ){
					futurePeriodVersionId = Integer.parseInt(strFuturePeriodVersionId);
				}else{
					if( futurePeriodsVersion != null && futurePeriodsVersion.size() > 0 ){
						cornFuturesVersionsView = (CornFuturesVersionsView)futurePeriodsVersion.get(0);
						futurePeriodVersionId = cornFuturesVersionsView.getFuturePeriodVersionId();
					}else{
						futurePeriodVersionId = -1;
					}
				}
			}
			purchaseOrderVersions = purchaseOrderVersionService.purchasesOrdersVersionsByMonth(versionId, futurePeriodVersionId);
			purchaseOrderVersionSortForm.setFuturePeriodVersionId(String.valueOf(futurePeriodVersionId));
			purchaseOrderVersionSortForm.setFutureSorts(purchaseOrderVersions);
			request.setAttribute(ATTRIBUTE_PERIODS_VERSION, futurePeriodsVersion);
			request.setAttribute(ATTRIBUTE_PURCHASES_ORDERS_VERSION, purchaseOrderVersions);
			request.setAttribute(ATTRIBUTE_PURCHASES_ORDERS_VERSION_SIZE, new Integer(purchaseOrderVersions.size()));
			request.setAttribute(ATTRIBUTE_FUTURE_PERIOD_VERSION_ID, String.valueOf(futurePeriodVersionId));
		}catch(CornFuturesVersionsException cornFuturesVersionsException){
			throw cornFuturesVersionsException;
		}catch(Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}
		return mapping.findForward(FORWARD_DEFAULT);
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws PurchaseProgramException {
		PurchaseOrderVersionSortForm purchaseOrderVersionSortForm = null;
		PurchaseOrderVersionService purchaseOrderVersionService = null;
		HttpSession httpSession = null;
		String strFuturePeriodVersionId = null;
		int futurePeriodVersionId = 0;
		try{
			purchaseOrderVersionSortForm = (PurchaseOrderVersionSortForm)form;
			httpSession = request.getSession();
			purchaseOrderVersionService =  new PurchaseOrderVersionService();
			strFuturePeriodVersionId = request.getParameter(PARAMETER_SAVE_FUTURE_PERIOD_VERSION_ID);
			futurePeriodVersionId = Integer.parseInt(strFuturePeriodVersionId);
			purchaseOrderVersionService.saveSort(futurePeriodVersionId, purchaseOrderVersionSortForm.getFutureSorts());
		}catch(Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}
		return mapping.findForward(FORWARD_DEFAULT);
	}
	
	public ActionForward originalSort(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws PurchaseProgramException {
		HttpSession httpSession = null;
		PurchaseOrderVersionService purchaseOrderVersionService = null;
		String strFuturePeriodVersionId = null;
		int futurePeriodVersionId = 0;
		try{
			httpSession = request.getSession();
			purchaseOrderVersionService = new PurchaseOrderVersionService();
			strFuturePeriodVersionId = request.getParameter(ATTRIBUTE_FUTURE_PERIOD_VERSION_ID);
			futurePeriodVersionId = Integer.parseInt(strFuturePeriodVersionId);
			purchaseOrderVersionService.originalSort(futurePeriodVersionId);
		}catch(Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}
		return mapping.findForward(FORWARD_DEFAULT);
	}
	
	public ActionForward move(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws PurchaseProgramException {
		HttpSession httpSession = null;
		PurchaseOrderVersionService purchaseOrderVersionService = null;
		String strPurchaseOrderVersionId = null;
		String strDirection = null;
		try{
			httpSession = request.getSession();
			purchaseOrderVersionService = new PurchaseOrderVersionService();
			strPurchaseOrderVersionId = request.getParameter(PARAMETER_PURCHASE_ORDER_VERSION_ID);
			strDirection = request.getParameter(PARAMETER_DIRECTION);
			purchaseOrderVersionService.moveSort(Integer.parseInt(strPurchaseOrderVersionId), Integer.parseInt(strDirection));
		}catch(Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}
		return mapping.findForward(FORWARD_DEFAULT);
	}
}